package com.k2data.k2app.domain.po.v1;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.k2data.k2app.domain.po.Customer;
import lombok.Data;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author clb
 */
@Data
@Table(name = "r_file")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RFile implements Serializable {

    @Transient
    private static final long serialVersionUID = 2582045611182509972L;

    @Id
    @GeneratedValue(generator = "JDBC")
    private Long id;
    private Long customerId;
    private Long deviceId;
    private Long eventId;
    private String fileName;
    private String fileUrl;
    private Long size;
    private String type;
    @JsonIgnore
    private ZonedDateTime createTime;
    @JsonIgnore
    private ZonedDateTime updateTime;


    /**
     * 转换格式
     * @param id
     * @param list
     * @return
     */
    @JsonIgnore
    @Transient
    public static List<RFile> updateFiles(Long id,List<Map> list,String type) {
        List<RFile> rFiles = new ArrayList<>();
            //更新文件
            if (list != null && list.size() > 0) {
                for (Map<String, String> map : list) {
                    RFile rFile = new RFile();
                    if("customer".equals(type)){
                        rFile.setCustomerId(id);
                    } else if("device".equals(type)){
                        rFile.setDeviceId(id);
                    } else if ("event".equals(type)){
                        rFile.setEventId(id);
                    }else if ("report".equals(type)){
                        rFile.setEventId(id);
                    }else{
                        rFile.setEventId(id);
                    }
                    rFile.setFileName(map.getOrDefault("name","").toString());
                    String url = map.get("url").toString();
                    if(url.indexOf("static/") == -1){
                        rFile.setFileUrl(url);
                    }else{
                        rFile.setFileUrl(url.substring(url.indexOf("static/") + "static/".length(), url.length()));
                    }
                    if (map.get("size") != null){
                        rFile.setSize(Long.parseLong(String.valueOf(map.get("size"))));
                    }
                    rFile.setType(type);
                    rFiles.add(rFile);
                }
            }
        return rFiles;
    }

}
